var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    // 用户地址   设为默认地址:use==true
    address: [],
  },
  // 手动添加按钮跳转
  addclick: function () {
    var that = this;
    wx.navigateTo({
      url: '../detail/index',
    })
  },

  // 设为默认地址
  setclick: function (e) {
    var that = this;

    var id = e.currentTarget.dataset.id;
    wx.request({
      url: app.url + 'api_user/isDefault',
      data: { id: id },
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      }, success: function (e) {
        console.log(e);
        prompt('设置成功！', 'success');
        that.onLoad(that.data);
      }
    })
  },
  //一次性使用的地址
  one_setclick: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var pages = getCurrentPages();
    var prevPage = pages[pages.length - 2]; //上一个页面
    prevPage.setData({
      address_id: id
    })
    wx.navigateBack()
  },
  // 编辑
  editor: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/address/detail/index?id=' + id,
    })
  },

  //  删除地址
  delet: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '地址删除，不可恢复！',
      success: function (res) {
        if (res.confirm) {
          wx.request({
            url: app.url + 'api_user/address_del',
            data: { id: id },
            header: {
              'content-type': 'application/json',
              'token': wx.getStorageSync('token_key')
            }, success: function (e) {
              that.onLoad(that.data);
            }
          })
        }
      }
    })
  },

  // 微信地址按钮
  mapclick: function () {
    var that = this;
    wx.chooseAddress({
      success: function (res) {
        // 添加至后台
        wx.request({
          url: app.url + 'api_user/addressEditV1',
          data: {
            accept_name: res.userName,
            province: res.provinceName,
            city: res.cityName,
            area: res.countyName,
            address: res.detailInfo,
            zip: res.postalCode,
            mobile: res.telNumber,
          },
          header: {
            'content-type': 'application/json',
            'token': wx.getStorageSync('token_key')
          }, success: function (json) {
            if (json.data.done == true) {
              prompt('提交成功！', 'success');
              that.onLoad(that.data)
            } else {
              prompt(json.data.message, 'loading');
            }
          }
        })
      }, fail: function (res) {
        wx.showModal({
          title: '温馨提醒',
          content: '需要获取您的地理位置才能使用小程序',
          cancelText: '取消',
          confirmText: '获取位置',
          success: function (res) {
            if (res.confirm) {
              getAuthor(that);
            } else if (res.cancel) {
              // getAuthor(that);
            }
          }
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var on_use = typeof (options.on_use) == 'undefined' ? 0 : options.on_use
    var is_order = typeof (options.is_order) == 'undefined' ? 0 : options.is_order
    wx.request({
      url: app.url + 'api_user/addressList',
      data: {},
      header: {
        'content-type': 'application/json',
        'token': wx.getStorageSync('token_key')
      },
      success: function (res) {
        var address = res.data.result;
        that.setData({ address: address })
        if (address.length > 0) {
          var defautIndex = address[0].id
          for (var i = 0; i < address.length; i++) {
            if (address[i].is_default == 1) {
              defautIndex = address[i].id
            }
          }
          that.setData({
            defautIndex: defautIndex,
            on_use: on_use,
            is_order: is_order,
            is_edit: 0
          })
        }

      },
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function (e) {
    var that = this
    if (that.data.is_edit == 1) {
      that.onLoad(that.data)
    }
    // that.onLoad({ is_order: that.data.is_order, on_use: that.data.on_use})
  },
})
function prompt(text, stat) {
  //提示信息
  wx.showToast({
    title: text,
    icon: stat,
    duration: 1200,
  })
};


function getAuthor(that) {
  wx.getSetting({
    success(res) {
      if (!res.authSetting['scope.userLocation']) {
        wx.openSetting({
          success: (res) => { }
        })
      }
    }
  })
}